<?php

class fvPager implements ArrayAccess, Iterator{
    private $_manager;
    private $_pageCount;
    private $_objects;
    private $_currentPage;
    private $_paramName;

    public function __construct($manager, $objects = null, $paramName = "page") {
        $this->_manager = $manager;
        $this->_objects = $objects;
        $this->_paramName = $paramName;
    }

    public function paginate($where = null, $order = null, $params = array(),$group = null) {

        if (!($objPerPage = fvSite::$fvConfig->get("modules." . fvRoute::getInstance()->getModuleName() . ".pager.show_per_page"))) {
            $objPerPage = fvSite::$fvConfig->get("pager.show_per_page")?fvSite::$fvConfig->get("pager.show_per_page"):10;
        }

        $this->_currentPage = fvRequest::getInstance()->getRequestParameter($this->_paramName, 'int') ? fvRequest::getInstance()->getRequestParameter($this->_paramName, 'int') : 0;

        if (!is_null($this->_objects)) {
            $count = count($this->_objects);
        } else {
            $count = $this->_manager->getCount($where, $params);
        }

        $this->_pageCount = ceil($count / $objPerPage);

        if (!is_null($this->_objects)) {
            $this->_objects = array_slice($this->_objects, $this->_currentPage * $objPerPage, $objPerPage);
        } else {
            $this->_objects = $this->_manager->getAll($where, $order,
                ((int)($this->_currentPage * $objPerPage)) . "," . $objPerPage, $params, null, $group);
        }

        return $this;
    }

    public function getData() {
        return $this->_objects;
    }

    public function hasPaginate() {
        return ($this->_pageCount > 1);
    }

    protected function _checkPage($pageNum) {
        if ($this->_pageCount < 10) return true;
        if (($pageNum < 3) || (($this->_pageCount - $pageNum) < 4)) return true;
        if (abs($pageNum - $this->_currentPage) < 3) return true;

        return false;
    }

    public function showPager ($full = false) {
        //echo $this->_currentPage;
        if ($this->_currentPage == $this->_pageCount-1)
        	$nextPage = $this->_currentPage;
        	else
			$nextPage = $this->_currentPage+1;

		if ($this->_currentPage != 0)
			$previousPage = $this->_currentPage - 1;
			else
			$previousPage = "0";
		$theLast = $this->_pageCount-1;
            $thisModule = fvRequest::getInstance()->getRequestParameter('module');
		$content = "
			<div style=\"padding-left:20px;\">		
			<form action=\"/$thisModule\"method=\"get\">
				<table style=\"margin-top: 20px; height: 20px;\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
                <tbody><tr>
                    <td style=\"height: 20px;\" valign=\"center\"><a href=\"?page=0\"><img src=\"img/page_vl.gif\" border=\"0\"></a></td>
                    <td style=\"height: 20px;\" valign=\"center\"><a href=\"?page=$previousPage\"><img src=\"img/page_l.gif\" border=\"0\"></a></td>
                    <td style=\"background-color: rgb(242, 242, 242); height: 1px;\" valign=\"center\"><a class=\"patha\">&nbsp;Сторінка&nbsp;</a></td>
                    <td style=\"background-color: rgb(242, 242, 242); height: 1px;\" valign=\"center\"><input style=\"font-size: 10px; width: 20px; height: 16px;\"name=\"newpage\" border=\"0\"value=\"".($this->_currentPage + 1)."\"></td>

                    <td style=\"background-color: rgb(242, 242, 242); height: 1px;\" valign=\"center\"><a class=\"patha\">&nbsp;з $this->_pageCount&nbsp;</a></td>
                    <td style=\"height: 20px;\" valign=\"center\"><a href=\"?page=$nextPage\"><img src=\"img/page_r.gif\" border=\"0\"></a></td>
                    <td style=\"height: 20px;\" valign=\"center\"><a href=\"?page=$theLast\"><img src=\"img/page_vr.gif\" border=\"0\"></a></td>
                    <td style=\"height: 20px;\" valign=\"center\"><a class=\"patha\">&nbsp;&nbsp;&nbsp;Знайдено ".$this->_manager->getCount($where, $params)." елементів</a></td>
                </tr>
                </tbody></table></form>
               </div>";


        return $content;
    }

    public function getCurrentPage() {
        return $this->_currentPage;
    }

    public function getPageCount() {
        return $this->_pageCount;
    }

    function offsetExists($offset) {
        return isset($this->_objects[$offset]);
    }

    function offsetGet($offset) {
        return $this->_objects[$offset];
    }

    function offsetUnset($offset) {
        unset($this->_objects[$offset]);
    }

    function offsetSet($offset, $newValue) {
        $this->_objects[$offset] = $newValue;
    }

    public function rewind() {
        reset($this->_objects);
    }

    public function current() {
        $var = current($this->_objects);
        return $var;
    }

    public function key() {
        $var = key($this->_objects);
        return $var;
    }

    public function next() {
        $var = next($this->_objects);
        return $var;
    }

    public function valid() {
        $var = $this->current() !== false;
        return $var;
    }

    public function getObjects() {
        return $this->_objects;
    }
}
